from aiogram import F
from aiogram_dialog import Dialog, Window
from aiogram_dialog.widgets.input import TextInput
from aiogram_dialog.widgets.kbd import Back, Button, Cancel, Group, ScrollingGroup, Select, Start
from aiogram_dialog.widgets.text import Format

from src.presentation.tg_bot.dialogs.getters.servers import server_type_getter
from src.presentation.tg_bot.dialogs.getters.users import user_ips_getter
from src.presentation.tg_bot.dialogs.widgets import LocaleText
from src.presentation.tg_bot.states import AddAllowedIPSG, ChangeAuthorizationSG

from .handlers import add_ip_address, select_ip_address, start_changing_authorization_type

authorization_types_window = Window(
    LocaleText("select-authorization-type"),
    Group(
        Button(
            LocaleText("by-ip-address"),
            id="by_ip_address",
            on_click=select_ip_address,
        ),
        Button(
            LocaleText("by-password"),
            id="by_password",
            on_click=start_changing_authorization_type,
            when=F["proxy_is_mobile_private"],
        ),
        width=2,
    ),
    Cancel(LocaleText("back-btn")),
    getter=server_type_getter,
    state=ChangeAuthorizationSG.select_authorization_type,
)

select_ip_address_window = Window(
    LocaleText("select-ip-address"),
    ScrollingGroup(
        Select(
            Format("{item.ip_address}"),
            "catselsel",
            lambda allowed_ip: allowed_ip.id,
            "allowed_ips",
            on_click=start_changing_authorization_type,
        ),
        width=2,
        height=4,
        id="catsel",
        hide_on_single_page=True,
    ),
    Start(
        LocaleText("add-ip-address"),
        id="create_ip_address",
        state=AddAllowedIPSG.enter_ip_address,
    ),
    Back(LocaleText("back-btn")),
    Cancel(LocaleText("cancel-btn")),
    getter=user_ips_getter,
    state=ChangeAuthorizationSG.select_ip_address,
)

authorization_types_dialog = Dialog(authorization_types_window, select_ip_address_window)
